// pages/putInPoint/pointIndex/pointIndex.js
const app = getApp();

// 引入SDK核心类
var QQMapWX = require('../../../../utils/qqmap-wx-jssdk.min.js');
var key = app.globalData.qqmapsdk;
var qqmapsdk = new QQMapWX({
  key: key
});
import Api from '../../../../utils/http';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchFlag: true,
    webSiteUrl: app.globalData.webSiteUrl,
    identityStatus: 1, //0为管理员身份，1为部门管理员
    tabBarIndex: 1, //当前tabBar索引显示
    currProvince: '选择省/市',
    currProvinceIndex: '',
    currCity: '选择市',
    currCityIndex: '',
    currArea: '选择区',
    currAreaIndex: '',
    provinceArr: [],
    cityArr: [],
    areaArr: [],
    longitude: '', //经度
    latitude: '', //纬度
    markers: [], //地图
    dataList: [], //最近
    often: [], //常用点
    launch: '', //关键字
    page: 1, //页码默认为1
    // region: ['选择省/市', '选择市', '选择区'],
  },
  //选择省/市/区
  // bindPlacePicker: function(e) {
  //   var that = this;
  //   var _index = e.detail.value;
  //   console.log(_index);
  //   console.log('picker发送选择改变，携带值为', e.detail.value)
  //   that.setData({
  //     region: _index
  //   })
  // },
  //选择省/市
  bindProvincePicker: function (e) {
    var that = this;
    var _index = e.detail.value;
    var {
      provinceArr
    } = that.data
    console.log(that.data)
    var pid = provinceArr[_index].id
    console.log('picker发送选择改变，携带值为', pid)
    that.setData({
      currProvinceIndex: provinceArr[_index].id,
      currProvince: that.data.provinceArr[_index].title
    });
    // that.getProvincePoint();
    that.getCity();
    that.getLaunchPoint();
  },
  //城市弹框
  bindCity: function () {
    var that = this;
    let currProvinceIndex = that.data.currProvinceIndex;
    if (currProvinceIndex == '') {
      wx.showToast({
        title: '请选择省/市',
      })
      return;
    } else {
      that.bindCityPicker();

    }
  },
  //地区弹框
  bindAreaArr: function () {
    var that = this;
    let currCityIndex = that.data.currCityIndex;
    if (currCityIndex == '') {
      wx.showToast({
        title: '请选择城市',
      })
      return;
    } else {
      that.bindAreaPicker();
      that.getProvincePoint();
    }
  },
  //选择市
  bindCityPicker: function (e) {
    var that = this;
    var _index = e.detail.value;
    var {
      cityArr
    } = that.data
    console.log(that.data)
    var cid = cityArr[_index].id
    console.log('picker发送选择改变，携带值为', cid)
    that.setData({
      currCityIndex: cityArr[_index].id,
      currCity: that.data.cityArr[_index].title
    });
    that.getArea();
    // that.getProvincePoint();
    that.getLaunchPoint();
  },

  //选择区
  bindAreaPicker: function (e) {
    var that = this;
    // let currCityIndex = that.data.currCityIndex;
    // let currProvinceIndex = that.data.currProvinceIndex;
    var _index = e.detail.value;
    var {
      areaArr
    } = that.data
    console.log(that.data)
    var aid = areaArr[_index].id
    console.log('picker发送选择改变，携带值为', aid)
    that.setData({
      currAreaIndex: areaArr[_index].id,
      currArea: that.data.areaArr[_index].title
    });
    that.getLaunchPoint();
  },

  //搜索
  toSearchKeyWord: function () {
    this.setData({
      searchFlag: false
    })
  },
  search: function (e) {
    var that = this;
    that.setData({
      launch: e.detail.value
    });
    this.getLaunchPoint();
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.getProvince();
    that.getCurrentLocation();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;

  },
  //查询投放点列表
  getPointList: function (e) {
    var that = this;
    var page = that.data.page;
    var datalist = that.data.dataList;
    var uid = wx.getStorageSync('userid');
    var param = {
      uid: uid.id,
      user_type: uid.user_type,
      pageNum: page.toString(),
      pageSize: '9',
      longitude: that.data.lng.toString(),
      latitude: that.data.lat.toString(),
    }
    console.log(param)
    Api.ajax("/app/Zlaunchpoint/pointlist", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        page++;
        that.setData({
          dataList: datalist.concat(res.data),
          page: page
        })
      } else {
        wx.showToast({
          title: '加载完成',
          icon: "none"
        })
      }
    })
  },
  //搜索投放点
  getLaunchPoint: function (e) {
    var that = this;
    var uid = wx.getStorageSync('userid');
    var param = {
      uid: uid.id,
      user_type: uid.user_type,
      pageNum: '1',
      pageSize: '10',
      longitude: that.data.lng.toString(),
      latitude: that.data.lat.toString(),
      province_id: that.data.currProvinceIndex,
      city_id: that.data.currCityIndex,
      county_id: that.data.currAreaIndex,
      title: that.data.launch,
    }
    console.log(param)
    // return;
    Api.ajax('/app/Zlaunchpoint/pointlist', param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          dataList: res.data
        })
      } else {
        wx.showToast({
          title: '暂无数据',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  // 获取省市列表
  getProvince: function () {
    var that = this;
    Api.ajax("/app/Zaccount/city").then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          provinceArr: res.data
        })

      }
    })
  },
  //获取城市列表
  getCity: function (e) {
    var that = this;
    var param = {
      id: that.data.currProvinceIndex,
    }
    console.log(param)
    Api.ajax("/app/Zaccount/city", param).then(res => {
      console.log(res)
      if (res.code === 200) {
        that.setData({
          cityArr: res.data
        })
        that.getArea()
      }
    })
  },
  //获取地区列表
  getArea: function (e) {
    var that = this;
    var param = {
      id: that.data.currCityIndex,
    }
    console.log(param)
    Api.ajax("/app/Zaccount/city", param).then(res => {
      console.log(res)
      if (res.code === 200) {
        that.setData({
          areaArr: res.data
        })
      }
    })
  },

  //获取地图投放信息
  getMap: function () {
    var that = this;
    let uid = wx.getStorageSync('userid');
    var param = {
      uid: uid.id,
      user_type: uid.user_type,
      pageNum: '1',
      pageSize: '10',
      longitude: that.data.lng.toString(),
      latitude: that.data.lat.toString(),
    };
    console.log(param)
    Api.ajax("/app/Zlaunchpoint/pointlist", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        let list = []
        res.data.forEach(item => {
          let obj = {
            width: 26,
            height: 26
          }
          obj['iconPath'] = item.img
          obj['id'] = item.id
          obj['latitude'] = item.latitude
          obj['longitude'] = item.longitude
          obj['label'] = {
            content: item.adress,
            anchorY: -13,
            anchorX: 13
          }
          list.push(obj)
        })
        console.log()
        that.setData({
          markers: list,
        })
      } else {

      }
    })
  },

  // 常用投放点列表展示信息
  // getOftenDetail: function () {
  //   var that = this;
  //   let uid = wx.getStorageSync('userid')
  //   var param = {
  //     uid: uid.id,
  //     user_type: uid.user_type,
  //     longitude: that.data.lng.toString(),
  //     latitude: that.data.lat.toString(),
  //   }
  //   console.log(param)
  //   Api.ajax("/app/Zlaunchpoint/oftenPoint", param).then(res => {
  //     console.log(res)
  //     if (res.code == 200) {
  //       this.setData({
  //         often: res.data
  //       });
  //     }
  //   })
  // },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    let uid = wx.getStorageSync('userid');
    var param = {
      uid: uid.id,
      user_type: uid.user_type,
      pageNum: '1',
      pageSize: '9',
      longitude: that.data.lng.toString(),
      latitude: that.data.lat.toString(),
    }
    console.log(param)
    Api.ajax("/app/Zlaunchpoint/pointlist", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          dataList: res.data,
          page: 2
        })
      }
      // 真机测试的时候，下拉刷新技术需要停止 下拉刷新的操作
      wx.stopPullDownRefresh();
    }).catch((err) => {
      console.log(err)
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getPointList();

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //获取当前定位
  getCurrentLocation: function () {
    var _this = this;
    qqmapsdk.reverseGeocoder({
      //位置坐标，默认获取当前位置，非必须参数
      //Object格式
      // location: {
      //   latitude: 39.984060,
      //   longitude: 116.307520
      // },
      //String格式
      // location: '39.984060,116.307520',

      //location: '', //获取表单传入的位置坐标,不填默认当前位置,示例为string格式
      // get_poi: 1, //是否返回周边POI列表：1.返回；0不返回(默认),非必须参数
      success: function (res) { //成功后的回调
        console.log(res);
        var result = res.result;
        wx.setStorageSync('latLng', result.location)
        _this.setData({
          lat: result.location.lat,
          lng: result.location.lng,
        })
        _this.getPointList();
        _this.getMap();
      },
      fail: function (error) {
        console.error(error);
      },
      complete: function (res) {
        // console.log(res);
        wx.hideLoading();
      }
    })
  },
})